
package Acciones.Persistencia;

import Configuraciones.Configuraciones;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;


public class AccionCancelar extends AbstractAction{
    private EntityManager em;
    private List ls;
    private javax.persistence.Query q;
    public static String NOMBRE="Cancelar";
    public static String DESCRIPCION="Cancelar los cambios realizados.";
    protected static int KEVENT=KeyEvent.VK_C;
    
    public AccionCancelar() {
            putValue(NAME, NOMBRE);
            putValue(SHORT_DESCRIPTION, DESCRIPCION);
            putValue(MNEMONIC_KEY, new Integer(KEVENT));
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KEVENT, 
                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            putValue(SMALL_ICON, Configuraciones.ICONO_CANCELAR);
    }

    public AccionCancelar(EntityManager em, List ls, Query q) {
        this();
        this.em = em;
        this.ls = ls;
        this.q = q;
    }
    
    

    @Override
    public void actionPerformed(ActionEvent e) {
        em.getTransaction().rollback();
        em.getTransaction().begin();
        Collection data= q.getResultList();
        for (Object object : data) em.refresh(object);
        ls.clear();
        ls.addAll(data);
    }

 
    public EntityManager getEm() {
        return em;
    }

 
    public void setEm(EntityManager em) {
        this.em = em;
    }

 
    public List getLs() {
        return ls;
    }

   
    public void setLs(List ls) {
        this.ls = ls;
    }

 
    public javax.persistence.Query getQ() {
        return q;
    }

  
    public void setQ(javax.persistence.Query q) {
        this.q = q;
    }
    
    
    
}
